<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item
        label="名称"
        prop="supplier"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.supplier" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item
        label="联系人"
        prop="contacts"
      >
        <el-input v-model="dataForm.contacts" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item
        label="手机号码"
        prop="telephone"
      >
        <el-input
          v-model="dataForm.telephone"
          placeholder="手机号码"
        ></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNum">
        <el-input v-model="dataForm.phoneNum" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="传真" prop="fax">
        <el-input v-model="dataForm.fax" placeholder="传真"></el-input>
      </el-form-item>
      <el-form-item label="预付款" prop="advanceIn">
        <el-input v-model="dataForm.advanceIn" placeholder="预付款"></el-input>
      </el-form-item>
      <el-form-item label="初期应收" prop="beginNeedGet">
        <el-input
          v-model="dataForm.beginNeedGet"
          placeholder="初期应收"
        ></el-input>
      </el-form-item>
      <el-form-item label="初期应付" prop="beginNeedPay">
        <el-input
          v-model="dataForm.beginNeedPay"
          placeholder="初期应付"
        ></el-input>
      </el-form-item>
      <el-form-item label="累计应收" prop="allNeedGet">
        <el-input
          v-model="dataForm.allNeedGet"
          placeholder="累计应收"
        ></el-input>
      </el-form-item>
      <el-form-item label="累计应付" prop="allNeedPay">
        <el-input
          v-model="dataForm.allNeedPay"
          placeholder="累计应付"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="税率" prop="taxRate">
        <el-input v-model="dataForm.taxRate" placeholder="税率"></el-input>
      </el-form-item> -->
      <el-form-item label="纳税人识别号" prop="taxNum">
        <el-input
          v-model="dataForm.taxNum"
          placeholder="纳税人识别号"
        ></el-input>
      </el-form-item>
      <el-form-item label="开户行" prop="bankName">
        <el-input v-model="dataForm.bankName" placeholder="开户行"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="accountNumber">
        <el-input
          v-model="dataForm.accountNumber"
          placeholder="账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="description">
        <el-input v-model="dataForm.description" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
            var validateTelephone = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(value)) {
        callback(new Error("请输入正确手机号码"));
      }
    };
    var validatePhoneNum = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (
        !(
          /^[1][3,4,5,7,8][0-9]{9}$/.test(value) ||
          /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(value)
        )
      ) {
        callback(new Error("请输入正确的联系电话"));
      }
    };
    var validateEmail = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (!/^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/.test(value)) {
        callback(new Error("请输入正确邮箱号"));
      }
    };
    var validateFax = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (!/^(\d{3,4}-)?\d{7,8}$/.test(value)) {
        callback(new Error("请输入正确传真号"));
      }
    };
    var validateAdvanceIn = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (!/^\d+$|^\d*\.\d+$/.test(value)) {
        callback(new Error("请输入正确的预付款，只能为数字"));
      }
    };
    var validateBeginNeedGet = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (!/^\d+$|^\d*\.\d+$/.test(value)) {
        callback(new Error("请输入正确的初期应收，只能为数字"));
      }
    };
    var validateBeginNeedPay = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (!/^\d+$|^\d*\.\d+$/.test(value)) {
        callback(new Error("请输入正确的初期应付，只能为数字"));
      }
    };
    var validateAllNeedGet = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (!/^\d+$|^\d*\.\d+$/.test(value)) {
        callback(new Error("请输入正确的累计应收，只能为数字"));
      }
    };
    // var validateTaxNum = (rule, value, callback) => {
    //   if (value == "") {
    //     callback();
    //   }
    //   if (!/^\d+\.{0,1}\d+%$/.test(value)) {
    //     callback(new Error("请输入正确的税率"));
    //   }
    // };
    // var validateTaxRate = (rule, value, callback) => {
    //   if (value == "") {
    //     callback();
    //   }
    //   if (!/^\d{16}$/.test(value)) {
    //     callback(new Error("请输入正确的纳税人识别号"));
    //   }
    // };
    var validateAccountNumber = (rule, value, callback) => {
      if (value == "") {
        callback();
      }
      if (!/^\d{16}$/.test(value)) {
        callback(new Error("请输入正确的账号"));
      }
    };
    return {
      parentValue: [],
      value1: "",
      value2: "",
      value: "",
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        supplier: "",
        contacts: "",
        telephone: "",
        email: "",
        fax: "",
        phoneNum: "",
        advanceIn: "",
        beginNeedGet: "",
        beginNeedPay: "",
        allNeedGet: "",
        allNeedPay: "",
        taxRate: "",
        advanceIn: "",
        taxNum: "",
        taxRate: "",
        bankName: "",
        accountNumber: "",
        address: "",
        description: "",
      },
        dataRule: {
                supplier: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        telephone: [
          { required: false, message: "请输入正确的手机号", trigger: "blur" },
          { validator: validateTelephone, trigger: "blur" },
        ],
        phoneNum: [
          { required: false, message: "请输入正确的联系电话", trigger: "blur" },
          { validator: validatePhoneNum, trigger: "blur" },
        ],
        email: [
          { required: false, message: "请输入正确的邮箱号", trigger: "blur" },
          { validator: validateEmail, trigger: "blur" },
        ],
        fax: [
          { required: false, message: "请输入正确的传真号", trigger: "blur" },
          { validator: validateFax, trigger: "blur" },
        ],
        advanceIn: [
          { required: false, message: "请输入正确的预付款", trigger: "blur" },
          { validator: validateAdvanceIn, trigger: "blur" },
        ],
        // beginNeedGet: [
        //   { required: false, message: "请输入正确的初期应收", trigger: "blur" },
        //   { validator: validateBeginNeedGet, trigger: "blur" },
        // ],
        // beginNeedPay: [
        //   { required: false, message: "请输入正确的初期应付", trigger: "blur" },
        //   { validator: validateBeginNeedPay, trigger: "blur" },
        // ],
        allNeedGet: [
          { required: false, message: "请输入正确的累计应收", trigger: "blur" },
          { validator: validateAllNeedGet, trigger: "blur" },
        ],
        // taxNum: [
        //   { required: false, message: "请输入正确的税率", trigger: "blur" },
        //   { validator: validateTaxNum, trigger: "blur" },
        // ],
        // taxRate: [
        //   { required: false, message: "请输入正确的税率", trigger: "blur" },
        //   { validator: validateTaxNum, trigger: "blur" },
        // ],
        accountNumber: [
          { required: false, message: "请输入正确的账号", trigger: "blur" },
          { validator: validateAccountNumber, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(index, id, pageIndex, pageSize, name, phone, number, remark) {
      // console.log(index, id, pageIndex, pageSize,name,phone,number,remark);
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/role/select"),
        method: "get",
        params: this.$http.adornParams(),
      })
        .then(({ data }) => {
          this.roleList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            let type = "供应商";
            let str = `{"supplier":"${name}","type":"${type}","phonenum":"${number}","telephone":"${phone}","description":"${remark}"}`;
            var search = encodeURIComponent(str);
            this.$http({
              url: this.$http.adornUrl(`/supplier/list?search=${search}`),
              method: "get",
              params: this.$http.adornParams({
                currentPage: pageIndex,
                pageSize: pageSize,
              }),
            }).then(({ data }) => {
              // console.log(data.data[index]);
              this.dataForm = data.data[index];
              this.dataForm.id = id;
            });
          }
        });
    },

    // 表单提交
    dataFormSubmit() {
      let isRequest = 0;
      this.$refs["dataForm"].validate((valid) => {
        console.log(valid);
        if (valid) {
        } else {
          isRequest = 1;
          return false;
        }
      });
      if (isRequest == 0) {
        this.submit();
      }
    },

        submit() {
      // console.log(!/^[1][3,4,5,7,8][0-9]{9}$/.test(this.dataForm.telephone));
      if (!(/^[1][3,4,5,7,8][0-9]{9}$/.test(this.dataForm.telephone)||!this.dataForm.telephone)) {
        return;
      }
      if (
        !(
          /^[1][3,4,5,7,8][0-9]{9}$/.test(this.dataForm.phoneNum) ||
          /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(this.dataForm.phoneNum)||!this.dataForm.phoneNum
        )
      ) {
        return;
      }
      if (!(/^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/.test(this.dataForm.email)||!this.dataForm.email)) {
        return;
      }
       console.log(333);
      if (!(/^(\d{3,4}-)?\d{7,8}$/.test(this.dataForm.fax)||!this.dataForm.fax)) {
        return;
      }
       console.log(444);
      if (!(/^\d+$|^\d*\.\d+$/.test(this.dataForm.advanceIn)||!this.dataForm.advanceIn)) {
        return;
      }
       console.log(555);
      if (!(/^\d+$|^\d*\.\d+$/.test(this.dataForm.beginNeedGet)||!this.dataForm.beginNeedGet)) {
        return;
      }
       console.log(666);
      if (!(/^\d+$|^\d*\.\d+$/.test(this.dataForm.beginNeedPay)||!this.dataForm.beginNeedPay)) {
        return;
      }
       console.log(777);
      if (!(/^\d+$|^\d*\.\d+$/.test(this.dataForm.allNeedGet)||!this.dataForm.allNeedGet)) {
        return;
      }
       console.log(888);
      // if (!this.dataForm.taxNum) {
      //   return;
      // }
       console.log(999);
               let info = {
            supplier: this.dataForm.supplier,
            contacts: this.dataForm.contacts,
            telephone: this.dataForm.telephone,
            email: this.dataForm.email,
            phonenum: this.dataForm.phoneNum,
            fax: this.dataForm.fax,
            BeginNeedGet: this.dataForm.beginNeedGet,
            BeginNeedPay: this.dataForm.beginNeedPay,
            AllNeedGet: this.dataForm.allNeedGet,
            AllNeedPay: this.dataForm.allNeedPay,
            taxNum: this.dataForm.taxNum,
            taxRate: this.dataForm.taxRate,
            bankName: this.dataForm.bankName,
            accountNumber: this.dataForm.accountNumber,
            address: this.dataForm.address,
            description: this.dataForm.description,
            type: "供应商",
            enabled: 1,
          };
          let url = this.$http.adornUrl("/supplier/add");
          if (this.dataForm.id) {
            url = this.$http.adornUrl(
              `/supplier/update?id=${this.dataForm.id}`
            );
          }
          this.$http({
            url,
            method: "post",
            data: this.$http.adornData(info, false),
          }).then(({ data }) => {
            console.log(data);
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
    },
  },
};
</script>
